//
//  UNIVoiceTopView.m
//  CAShapeLayerVoiceView
//
//  Created by 宇宙执政 on 16/11/4.
//  Copyright © 2016年 yujianbinMAC. All rights reserved.
//

#import "UNIVoiceTopView.h"
#import "UIView+UNIViewExtension.h"

#define UNI_PI 3.14159265359
#define UNI_RADIANS(number)  ((UNI_PI * number)/ 180)

@interface UNIVoiceTopView()


/**
 话筒线的view
 */
@property(nonatomic, strong) UIView *outsideLineView;

/**
 话筒线的layer
 */
@property(nonatomic, strong) CAShapeLayer *outsideLineLayer;


/**
 话筒view
 */
@property(nonatomic, strong) UIView *solidView;


/**
 话筒layer
 */
@property(nonatomic, strong) CAShapeLayer *solidLayer;


/**
 圆弧view
 */
@property(nonatomic, strong) UIView *arcView;


/**
 圆弧layer
 */
@property(nonatomic, strong) CAShapeLayer *arcLayer;

/**
 线宽
 */
@property(nonatomic, assign) CGFloat lineWith;


/**
 线的颜色
 */
@property(nonatomic, strong) UIColor *lineColor;


/**
 实心颜色
 */
@property(nonatomic, strong) UIColor *solidColor;
@end

@implementation UNIVoiceTopView

-(instancetype)initWithFrame:(CGRect)frame lineWith:(CGFloat)lineWith lineColor:(UIColor *)lColor solidColor:(UIColor *)sColor{
    
    if (self = [super initWithFrame:frame]) {
        _lineWith = lineWith;
        _lineColor = lColor;
        _solidColor = sColor;
     
        [self setup];
    }
    return self;
}

-(void)setup{
    
    //1，话筒内部
    _solidView = [[UIView alloc] initWithFrame:CGRectMake(self.width*0.38, self.height*0.15, self.width*0.24, self.height*0.7)];
    _solidView.layer.cornerRadius = _solidView.width*0.4;
    _solidView.clipsToBounds = YES;
    _solidLayer = [self drawOutsideLine:_solidView.bounds color:[UIColor whiteColor] ifFill:YES];
    //TODO: 添加话筒实心layer
    [_solidView.layer addSublayer:_solidLayer];
    [self addSubview:_solidView];
    
    //2,话筒边框
    _outsideLineView = [[UIView alloc] initWithFrame:CGRectMake(self.width*0.38, self.height*0.15, self.width*0.24, self.height*0.7)];
    _outsideLineLayer = [self drawOutsideLine:_outsideLineView.bounds color:[UIColor whiteColor] ifFill:NO];
    //添加话筒边框layer
    [_outsideLineView.layer addSublayer:_outsideLineLayer];
    [self addSubview:_outsideLineView];
    
    
    //3,画圆弧
    _arcView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height*0.1, self.width, self.height*0.6)];
    //添加圆弧layer
    _arcLayer = [self drawARCLine:_arcView.center frame:_arcView.frame color:_lineColor];
    [_arcView.layer addSublayer:_arcLayer];
    [self addSubview:_arcView];
}




/**
 绘制实心边框

 @param frame <#frame description#>
 @param color <#color description#>
 @param fill <#fill description#>
 @return <#return value description#>
 */
-(CAShapeLayer *)drawOutsideLine:(CGRect)frame color:(UIColor *)color ifFill:(BOOL)fill{
    CAShapeLayer *layer = [CAShapeLayer layer];
    if (fill) {
        layer.strokeColor = nil;
        layer.fillColor = color.CGColor;
    }else{
        layer.strokeColor = color.CGColor;
        layer.fillColor = nil;
    }
    
    layer.frame = frame;
    layer.lineWidth =_lineWith;
    layer.lineCap = kCALineCapRound;
    
    layer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, frame.size.width, frame.size.height) cornerRadius:frame.size.width * 0.4].CGPath;
    
    return layer;
}

/**
 绘制底部圆弧

 @param point <#point description#>
 @param frame <#frame description#>
 @param color <#color description#>
 @return <#return value description#>
 */
-(CAShapeLayer *)drawARCLine:(CGPoint)point frame:(CGRect)frame color:(UIColor *)color{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor = color.CGColor;//边框颜色
    shapeLayer.frame = frame;
    shapeLayer.lineWidth = _lineWith;
    shapeLayer.lineCap = kCALineCapRound;
    
    shapeLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(point.x, point.y) radius:frame.size.width*0.3 startAngle:UNI_RADIANS(-5) endAngle:UNI_RADIANS(185) clockwise:YES].CGPath;
    
    return shapeLayer;
}

-(void)updateVoiceViewWithVolume:(CGFloat)volume{
    
    CGFloat heignt = _solidView.height;
    CGFloat newHeight = heignt * volume;
    CGFloat width = _solidView.width;
    
    _solidLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, heignt-newHeight, width, heignt) cornerRadius:0].CGPath;
    
}

@end
